package cn.itcast.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.constants.Constants;
import cn.itcast.crm.domain.Customer;

@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Controller
public class LoginAction extends BaseAction<Customer> {

	@Action(value = "customerLogin", results = {
			@Result(name = "success", location = "index.html#/myhome", type = "redirect"),
			@Result(name = "login", location = "login.html", type = "redirect") })
	public String login() {
		Customer customer = WebClient
				.create(Constants.CRM_MANAGEMENT_HOST+"/crm_management/services/customerService/customer/"
				+ "login?telephone="+ model.getTelephone() + "&password="+ model.getPassword())
				.accept(MediaType.APPLICATION_JSON).get(Customer.class);
		
		if (customer == null) {
			return LOGIN;
		} 
		ServletActionContext.getRequest().getSession().setAttribute("customer", customer);
		return SUCCESS;
	}
}
